package com.byxy.interceptor;

import com.byxy.model.AdminUser;
import com.byxy.service.AdminUserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

public class LoginInterceptor extends MethodFilterInterceptor {
    @Autowired
    private AdminUserService adminUserService;
    @Override
    protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
        ActionContext ac = ActionContext.getContext();
        Map session = ac.getSession();
        AdminUser adminUser = (AdminUser) session.get("adminUser");
        if (adminUser == null) {
            return "login";
        }
        else {
            if (!adminUserService.hasRule(adminUser)) {
                return "error";
            }
        }
        return actionInvocation.invoke();
    }

}
